<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
    :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="草地宗地号" prop="grasslandParcelNumber">
            <el-input v-model="form.grasslandParcelNumber" placeholder="请输入草地宗地号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="草权证号" prop="draftCertificateNumber">
            <el-input v-model="form.draftCertificateNumber" placeholder="请输入草权证号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="不动产单元号" prop="realEstateUnitNumber">
            <el-input v-model="form.realEstateUnitNumber" placeholder="请输入不动产单元号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="草原坐落县" prop="grasslandLocatedInTheCounty">
            <el-input v-model="form.grasslandLocatedInTheCounty" placeholder="请输入草原坐落县"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="草原坐落乡/镇/街道" prop="grasslandLocatedInTownshiptownstreet">
            <el-input v-model="form.grasslandLocatedInTownshiptownstreet" placeholder="请输入草原坐落乡/镇/街道"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="草原坐落村" prop="grasslandLocatedVillage">
            <el-input v-model="form.grasslandLocatedVillage" placeholder="请输入草原坐落村"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="草原坐落组" prop="grasslandLocationGroup">
            <el-input v-model="form.grasslandLocationGroup" placeholder="请输入草原坐落组"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="草权地所有权权利人" prop="cqdsyqqlr">
            <el-input v-model="form.cqdsyqqlr" placeholder="请输入草权地所有权权利人"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="林地所有权权利人统一社会信用代码" prop="fforestLandOwnershipRightsHolders">
            <el-input v-model="form.fforestLandOwnershipRightsHolders" placeholder="请输入林地所有权权利人统一社会信用代码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="权证类型" prop="typeOfWarrant">
            <el-input v-model="form.typeOfWarrant" placeholder="请输入权证类型"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="权证发证日期" prop="certificateIssuanceDate">
            <el-date-picker type="date" placeholder="请选择权证发证日期" v-model="form.certificateIssuanceDate" :value-format="dateStr"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="权证发证机关" prop="certificateIssuingAuthority">
            <el-input v-model="form.certificateIssuingAuthority" placeholder="请输入权证发证机关"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="权证有效期" prop="validityPeriodOfWarrant">
            <el-input v-model="form.validityPeriodOfWarrant" placeholder="请输入权证有效期"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="权证图片" prop="certificateImage">
            <el-input v-model="form.certificateImage" placeholder="请输入权证图片"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="所有权经办机构" prop="ownershipAgency">
            <el-input v-model="form.ownershipAgency" placeholder="请输入所有权经办机构"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="所有权经办材料" prop="ownershipHandlingMaterials">
            <el-input v-model="form.ownershipHandlingMaterials" placeholder="请输入所有权经办材料"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="所有权经办系统" prop="ownershipHandlingSystem">
            <el-input v-model="form.ownershipHandlingSystem" placeholder="请输入所有权经办系统"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="所有权经办系统地址" prop="ownershipHandlingSystemAddress">
            <el-input v-model="form.ownershipHandlingSystemAddress" placeholder="请输入所有权经办系统地址"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="审核人" prop="reviewer">
            <el-input v-model="form.reviewer" placeholder="请输入审核人"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="审核人编号" prop="auditorNumberAuditProcess">
            <el-input v-model="form.auditorNumberAuditProcess" placeholder="请输入审核人编号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="依据文件编号" prop="accordingToTheDocumentNumber">
            <el-input v-model="form.accordingToTheDocumentNumber" placeholder="请输入依据文件编号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="依据文件名称" prop="accordingToTheFileName">
            <el-input v-model="form.accordingToTheFileName" placeholder="请输入依据文件名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="状态" prop="state">
            <el-input v-model="form.state" placeholder="请输入状态"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="GrassRightsInformationDialog">
// ========== 1. 导入语句 ==========
import { useDict } from '/@/hooks/dict';
import { rule } from '/@/utils/validate';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, validateExist } from '/@/api/forestry/grassRightsInformation';

// ========== 2. 组件定义 ==========
// 定义组件事件
const emit = defineEmits(['refresh']);

// ========== 3. 响应式数据定义 ==========
// 基础响应式变量
const dataFormRef = ref(); // 表单引用
const visible = ref(false); // 弹窗显示状态
const loading = ref(false); // 加载状态

// 表单数据对象
const form = reactive({
  id: '', // 主键
  grasslandParcelNumber: '', // 草地宗地号
  draftCertificateNumber: '', // 草权证号
  realEstateUnitNumber: '', // 不动产单元号
  grasslandLocatedInTheCounty: '', // 草原坐落县
  grasslandLocatedInTownshiptownstreet: '', // 草原坐落乡/镇/街道
  grasslandLocatedVillage: '', // 草原坐落村
  grasslandLocationGroup: '', // 草原坐落组
  cqdsyqqlr: '', // 草权地所有权权利人
  fforestLandOwnershipRightsHolders: '', // 林地所有权权利人统一社会信用代码
  typeOfWarrant: '', // 权证类型
  certificateIssuanceDate: '', // 权证发证日期
  certificateIssuingAuthority: '', // 权证发证机关
  validityPeriodOfWarrant: '', // 权证有效期
  certificateImage: '', // 权证图片
  ownershipAgency: '', // 所有权经办机构
  ownershipHandlingMaterials: '', // 所有权经办材料
  ownershipHandlingSystem: '', // 所有权经办系统
  ownershipHandlingSystemAddress: '', // 所有权经办系统地址
  reviewer: '', // 审核人
  auditorNumberAuditProcess: '', // 审核人编号
  accordingToTheDocumentNumber: '', // 依据文件编号
  accordingToTheFileName: '', // 依据文件名称
  state: '', // 状态
});

// ========== 4. 字典数据处理 ==========

// ========== 5. 表单校验规则 ==========
const dataRules = ref({
  grasslandParcelNumber: [
    { required: true, message: '草地宗地号不能为空', trigger: 'blur' }
  ],
  draftCertificateNumber: [
    { required: true, message: '草权证号不能为空', trigger: 'blur' }
  ],
  cqdsyqqlr: [
    { required: true, message: '草权地所有权权利人不能为空', trigger: 'blur' }
  ],
});

// ========== 6. 方法定义 ==========
// 获取详情数据
const getGrassRightsInformationData = async (id: string) => {
  try {
    loading.value = true;
    const { data } = await getObj({ id: id });
    // 直接将第一条数据赋值给表单
    Object.assign(form, data[0]);
  } catch (error) {
    useMessage().error('获取数据失败');
  } finally {
    loading.value = false;
  }
};

// 打开弹窗方法
const openDialog = (id: string) => {
  visible.value = true;
  form.id = '';

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取GrassRightsInformation信息
  if (id) {
    form.id = id;
    getGrassRightsInformationData(id);
  }
};

// 提交表单方法
const onSubmit = async () => {
  loading.value = true; // 防止重复提交
  
  // 表单校验
  const valid = await dataFormRef.value.validate().catch(() => {});
  if (!valid) {
    loading.value = false;
    return false;
  }

  try {
    // 根据是否有ID判断是新增还是修改
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh'); // 通知父组件刷新列表
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// ========== 7. 对外暴露 ==========
// 暴露方法给父组件
defineExpose({
  openDialog
});
</script> 